

def select_func():
    import random
    wh = {'sock':10, 'shoes':20, 'slippers':30, 'necklace':40}

    total = sum(wh.values())
    rannum = random.randint(1, total)

    if rannum < wh['sock'] + 1:
        select =  'sock'
    elif rannum < wh['sock'] + wh['shoes'] + 1:
        select = 'shoes'
    elif rannum < wh['sock'] + wh['shoes'] + wh['slippers'] + 1:
        select = 'slippers'
    else:
        select = 'necklace'

    return '商品名称为:{}'.format(select)

# 函数调用
print(select_func())

# 老师尝试了运行100，1000，10000次该函数，误差在可接受的范围内，但是次数越小，误差越大，可以尝试一下减小误差看看。老师的测试代码如下：
temp = {}
for i in range(100000):
    result = select_func()
    if temp.get(result) is None:
        temp[result] = 1
    else:
        temp[result] += 1
print(temp)